using System;

namespace StockPresenter
{
    /// <summary>
    /// 表示股票数据模型，包含股票基本信息和技术指标
    /// </summary>
    public class Stock
    {
        private string _code;
        private string _name;
        private double _price;
        private double _changePercent;
        private double _volume;
        private string _date;

        /// <summary>
        /// 股票代码
        /// - 自动去除前后空格
        /// - 空值自动转为"N/A"
        /// </summary>
        public string Code 
        {
            get => _code;
            set => _code = string.IsNullOrWhiteSpace(value) ? "N/A" : value.Trim();
        }

        /// <summary>
        /// 股票名称
        /// - 自动去除前后空格
        /// - 空值自动转为"未知股票"
        /// </summary>
        public string Name 
        {
            get => _name;
            set => _name = string.IsNullOrWhiteSpace(value) ? "未知股票" : value.Trim();
        }

        /// <summary>
        /// 当前价格(元)
        /// - 自动四舍五入到小数点后2位
        /// - 负值自动转为0
        /// </summary>
        public double Price 
        {
            get => _price;
            set => _price = value >= 0 ? Math.Round(value, 2) : 0;
        }

        /// <summary>
        /// 涨跌幅百分比(%)
        /// - 正值表示上涨，负值表示下跌
        /// - 自动四舍五入到小数点后2位
        /// </summary>
        public double ChangePercent 
        {
            get => _changePercent;
            set => _changePercent = Math.Round(value, 2);
        }

        public double Volume 
        {
            get => _volume;
            set => _volume = value >= 0 ? value : 0;
        }

        public string Date 
        {
            get => _date;
            set => _date = string.IsNullOrWhiteSpace(value) ? DateTime.Now.ToString("yyyy-MM-dd") : value.Trim();
        }

        private double _kValue = 50;
        private double _dValue = 50;
        private double _jValue = 50;

        /// <summary>
        /// KDJ指标-K值
        /// - 计算周期: 9日
        /// - 自动四舍五入到小数点后2位
        /// - 典型范围: 0-100
        /// - 超买区: >80, 超卖区: <20
        /// </summary>
        public double KValue 
        {
            get => _kValue;
            set => _kValue = Math.Round(value, 2);
        }

        /// <summary>
        /// KDJ指标-D值
        /// - 计算周期: 3日K值的移动平均
        /// - 自动四舍五入到小数点后2位
        /// - 典型范围: 0-100
        /// - 超买区: >80, 超卖区: <20
        /// </summary>
        public double DValue 
        {
            get => _dValue;
            set => _dValue = Math.Round(value, 2);
        }

        /// <summary>
        /// KDJ指标-J值
        /// - 计算公式: 3K-2D
        /// - 自动四舍五入到小数点后2位
        /// - 波动范围更大,可超过100或低于0
        /// </summary>
        public double JValue 
        {
            get => _jValue;
            set => _jValue = Math.Round(value, 2);
        }

        private double _dif = 0;
        private double _dea = 0;
        private double _macd = 0;

        public double DIF 
        {
            get => _dif;
            set => _dif = Math.Round(value, 2);
        }

        public double DEA 
        {
            get => _dea;
            set => _dea = Math.Round(value, 2);
        }

        public double MACD 
        {
            get => _macd;
            set => _macd = Math.Round(value, 2);
        }

        public override string ToString()
        {
            return $"{Code} {Name} 价格:{Price:F2}元 涨幅:{ChangePercent:F2}% 成交量:{Volume/10000:F0}万手 日期:{Date}";
        }

        public string ToShortString()
        {
            return $"{Code} {Name} {ChangePercent:F2}%";
        }

        public string ToDetailString()
        {
            return $"股票代码: {Code}\n名称: {Name}\n当前价: {Price:F2}元\n涨幅: {ChangePercent:F2}%\n成交量: {Volume/10000:F0}万手\n日期: {Date}";
        }
    }
}